package com.mk.chromeapp;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    private class MyWebViewClient extends WebViewClient{

    }

    private class MyWebChromeClient extends WebChromeClient{

    }
    public ValueCallback<Uri> mUploadMessage;
    public ValueCallback<Uri[]> mUploadMessageFroAndroid5;
    public final static int FILE_CHOOSE_RESULT_CODE_FOR_ANDROID_5=2;
    public final static int FILE_CHOOSE_RESULT_CODE=1;
    private Uri imageUri;


    private void openFileChooserImplForAndroid5(ValueCallback<Uri[]> filePathCallback){
        mUploadMessageFroAndroid5 = filePathCallback;
        Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
        contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
        contentSelectionIntent.setType("image/*");
        startActivityForResult(Intent.createChooser(contentSelectionIntent,"Image Chooser"),FILE_CHOOSE_RESULT_CODE_FOR_ANDROID_5);

//        Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
//        chooserIntent.putExtra(Intent.EXTRA_INTENT,contentSelectionIntent);
//        chooserIntent.putExtra(Intent.EXTRA_INTENT,"Image Chooser");
//        startActivityForResult(chooserIntent, FILE_CHOOSE_RESULT_CODE_FOR_ANDROID_5);

    }
    private void openFileChooserImpl(ValueCallback<Uri> filePathCallback){
        mUploadMessage = filePathCallback;
        Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
        contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
        contentSelectionIntent.setType("image/*");
        startActivityForResult(Intent.createChooser(contentSelectionIntent,"File Chooser"),FILE_CHOOSE_RESULT_CODE);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
        Uri result = data==null?null:data.getData();//(intent == null || requestCode != Activity.RESULT_OK)?null:intent.getData();
//        Uri result = null;
        switch (requestCode){
            case FILE_CHOOSE_RESULT_CODE_FOR_ANDROID_5:
                if(null == mUploadMessageFroAndroid5){
                    return;
                }
//                if(result != null){
//                    mUploadMessageFroAndroid5.onReceiveValue(new Uri[]{result});
//                }else {
//                    mUploadMessageFroAndroid5.onReceiveValue(new Uri[]{});
//                }
                if(result != null){
                    mUploadMessageFroAndroid5.onReceiveValue(new Uri[]{result});
                }else {
                    mUploadMessageFroAndroid5.onReceiveValue(new Uri[]{});
                }
                mUploadMessageFroAndroid5 = null;
                break;
            case FILE_CHOOSE_RESULT_CODE:
                if(null == result){
                    return;
                }
                mUploadMessage.onReceiveValue(result);
                mUploadMessage = null;
                break;
        }
    }

    private void initViews(){
        webView = findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setAllowContentAccess(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.setWebViewClient(new WebViewClient(){

        });
        webView.setWebChromeClient(new WebChromeClient(){
//            public boolean openFileChooserIml(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
//                openFileChooserImpl(uploadMsg);
//                return false;
//            }

            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> uplodMsg,
                                             WebChromeClient.FileChooserParams fileChooserParams){
                openFileChooserImplForAndroid5(uplodMsg);
                return true;
            }
        });
        webView.loadUrl("http://192.168.1.66");
//        webView.loadUrl("http://10.2.7.92:8080/?pad=1#/login");

//        webView.loadUrl("http://10.2.7.92:3005");

    }
}